package com.hks360.car_treasure.mvp.user.model;

import android.content.Context;

import com.hks360.car_treasure.manager.CommonDataManager;
import com.hks360.car_treasure.model.BaseModel;
import com.hks360.car_treasure.mvp.BaseModelListener;
import com.hks360.car_treasure.util.GsonParseUtil;
import com.hks360.car_treasure.util.GsonUtil;
import com.hks360.library.nohttp.HttpCallback;
import com.hks360.nohttp.Response;
import com.hks360.car_treasure.model.Add.User;
/**
 * Created by xiaote on 2016/3/29.
 */
public class UserModelImpl implements UserModel {
    private Context context;

    public UserModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public void login(int what,String mobile, String password, final LoginListener listener) {
        HttpCallback httpCallback = new HttpCallback<String>() {
            @Override
            public void onSuccess(int what, Response<String> response) {
                User user = GsonParseUtil.parseUser(response.get());
                listener.onSuccess(what,user);
            }

            @Override
            public void onFailed(int what) {
                listener.onFailed(what);
            }

            @Override
            public void onStart(int what) {
                listener.onStart(what);
            }

            @Override
            public void onFinish(int what) {
                listener.onFinish(what);
            }
        };
        CommonDataManager.getInstance(context).userLog(what, mobile, password, httpCallback);
    }

    @Override
    public void regist(int what,String mobile, String password, String code,final LoginListener listener) {
        HttpCallback<String> httpCallback = new HttpCallback<String>() {
            @Override
            public void onSuccess(int what, Response<String> response) {
                User user = GsonParseUtil.parseUser(response.get());
                listener.onSuccess(what,user);
        }

            @Override
            public void onFailed(int what) {
                listener.onFailed(what);
            }

            @Override
            public void onStart(int what) {
                listener.onStart(what);
            }

            @Override
            public void onFinish(int what) {
                listener.onFinish(what);
            }
        };
        CommonDataManager.getInstance(context).userRegister(what,mobile,password,code,httpCallback);
    }

    @Override
    public void sendCheckCode(int what, String mobile, final BaseModelListener listener) {
        HttpCallback<String> httpCallback = new HttpCallback<String>() {
            @Override
            public void onSuccess(int what, Response<String> response) {
                BaseModel baseModel = GsonUtil.parseJson(response.get(),BaseModel.class);
                listener.onSuccess(what,baseModel);
            }

            @Override
            public void onFailed(int what) {
                listener.onFailed(what);
            }

            @Override
            public void onStart(int what) {
                listener.onStart(what);
            }

            @Override
            public void onFinish(int what) {
                listener.onFinish(what);
            }
        };
        CommonDataManager.getInstance(context).sendCheckCode(what,mobile,httpCallback);
    }

    @Override
    public void changePassword(int what, String mobile, String checkCode, String newpassword, final BaseModelListener listener) {
        HttpCallback<String> httpCallback = new HttpCallback<String>() {
            @Override
            public void onSuccess(int what, Response<String> response) {
                BaseModel model = GsonParseUtil.parseBase(response.get());
                listener.onSuccess(what,model);
            }

            @Override
            public void onFailed(int what) {
                listener.onFailed(what);
            }

            @Override
            public void onStart(int what) {
                listener.onStart(what);
            }

            @Override
            public void onFinish(int what) {
                listener.onFinish(what);
            }
        };
        CommonDataManager.getInstance(context).userChangePasswordCheckCode(what,mobile,checkCode,newpassword,httpCallback);
    }

    public interface LoginListener {

        void onSuccess(int what,User user);

        void onFailed(int what);

        void onStart(int what);

        void onFinish(int what);
    }

}